package com.net128.beatportapps.downloader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JProgressBar;
import javax.swing.table.AbstractTableModel;

class DownloadsTableModel extends AbstractTableModel implements Observer {
	private static final long serialVersionUID = 1L;
	private static final String[] columnNames = { "File", "Progress", "Status" };
	private static final Class[] columnClasses = { String.class, JProgressBar.class, String.class };
	private ArrayList<DownloadWorker> downloadList = new ArrayList<DownloadWorker>();
	private boolean sort=false;

	public void addDownload(DownloadWorker download) {
		download.addObserver(this);
		downloadList.add(download);
		fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
	}

	public void sortingChanged() {
		sort=true;
	}
	
	public DownloadWorker [] getDownloads() {
		return (DownloadWorker []) downloadList.toArray(new DownloadWorker [downloadList.size()]);
	}
/*	
	public DownloadWorker getDownload(int row) {
		return (DownloadWorker) downloadList.get(row);
	}
*/
	public void clearDownload(int row) {
		downloadList.remove(row);
		fireTableRowsDeleted(row, row);
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}
	
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int col) {
		return columnClasses[col];
	}

	public int getRowCount() {
		return downloadList.size();
	}

	public Object getValueAt(int row, int col) {
		DownloadWorker download = downloadList.get(row);
		switch (col) {
		case 0: // URL
			String file=download.getFile();
			/*int pos=file.lastIndexOf("__");
			if(pos>0) {
				file=file.substring(pos+2);
			}*/
			return file;
		case 1: // Progress
			return new Float(download.getProgress());
		case 2: // Status
			return DownloadWorker.STATUSES[download.getStatus()];
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public void update(Observable o, Object arg) {
		class DownloadComparator implements Comparator {
			public int compare(Object o1, Object o2) {
				DownloadWorker dw1=(DownloadWorker)o1;
				DownloadWorker dw2=(DownloadWorker)o2;
				int s1=dw1.getStatus();
				int s2=dw2.getStatus();
				if(s1==DownloadWorker.DOWNLOADING) {
					if(s2==DownloadWorker.DOWNLOADING) {
						return Double.compare(dw1.getStarted(), dw2.getStarted());
					}
					return 0;
				} else if(s2==DownloadWorker.DOWNLOADING) {
					return 1;
				} else if(s1==s2) {
					int result=dw1.getFile().compareTo(dw2.getFile());
					if(result!=0) {
						return dw1.getFile().compareTo(dw2.getFile());
					}
				}
				return Double.compare(s1, s2);
			}
		};

		if(sort) {
			Collections.sort(downloadList,new DownloadComparator());
			fireTableDataChanged();
		}
		sort=false;
		int index = downloadList.indexOf(o);
		fireTableRowsUpdated(index, index);
	}
}
